package com.luocc.ccdemo.ui

import android.app.AlertDialog
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.luocc.ccdemo.R
import com.luocc.ccdemo.vm.UserViewModel
import com.luocc.ccdemo.widget.EditTextController

class RegisterFragment : Fragment() {
    private lateinit var mUsername: EditText
    private lateinit var mPassword: EditText
    private lateinit var mRePassword: EditText
    private lateinit var mUsernameTips: TextView
    private lateinit var mPasswordTips: TextView
    private lateinit var mRePasswordTips: TextView
    private lateinit var mRegister: Button
    private var mProgressDialog: AlertDialog? = null

    private var mUsernameState = false
    private var mPasswordState = false
    private lateinit var mUserViewModel: UserViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
        mUserViewModel.getRegisterResult().observe(this) {
            mProgressDialog?.cancel()
            if (it.code == 0) {
                Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show()
                findNavController().popBackStack()
            } else {
                Toast.makeText(context, "注册失败:${it.message}", Toast.LENGTH_LONG).show()
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val parent = inflater.inflate(R.layout.fragment_register, container, false)
        mUsername = parent.findViewById(R.id.username)
        mPassword = parent.findViewById(R.id.password)
        mRePassword = parent.findViewById(R.id.re_password)
        mUsernameTips = parent.findViewById(R.id.username_tips)
        mPasswordTips = parent.findViewById(R.id.password_tips)
        mRePasswordTips = parent.findViewById(R.id.re_password_tips)
        mRegister = parent.findViewById(R.id.register)
        mProgressDialog = AlertDialog.Builder(context)
            .setView(R.layout.layout_dialog_progress)
            .setCancelable(false)
            .create()

        checkRegisterState()

        // username
        EditTextController()
            .setEditText(mUsername)
            .setTextView(mUsernameTips)
            .setTips(resources.getString(R.string.username_tips))
            .setMinLength(5)
            .setMaxLength(20)
            .setAvoidRegex("[^0-9a-zA-Z]")
            .setOnStateChangedListener {
                mUsernameState = it
                checkRegisterState()
            }

        // password
        EditTextController()
            .setEditText(mPassword)
            .setTextView(mPasswordTips)
            .setTips(resources.getString(R.string.password_tips))
            .setMinLength(8)
            .setMaxLength(16)
            .setAvoidRegex("[^0-9a-zA-Z]")
            .setOnStateChangedListener {
                mPasswordState = it
                checkRegisterState()
            }

        // re password
        EditTextController()
            .setEditText(mRePassword)
            .setTips(resources.getString(R.string.re_password_tips))
            .setOnStateChangedListener {
                checkRegisterState()
            }

        mRegister.setOnClickListener {
            Log.d(TAG, "mRegister: click")
            val username = mUsername.text.toString()
            val password = mPassword.text.toString()
            mProgressDialog?.show()
            mUserViewModel.register(username, password, password)
        }
        return parent
    }

    private fun checkRegisterState() {
        val same = mPassword.text.toString() == mRePassword.text.toString()
        if (mPasswordState && mRePassword.length() > 0) {
            if (!same) {
                val right = ResourcesCompat.getDrawable(
                    resources,
                    R.mipmap.ic_warning,
                    null
                )
                right?.setBounds(0, 0, mRePassword.height / 2, mRePassword.height / 2)
                mRePasswordTips.setText(R.string.re_password_tips_error)
                mRePasswordTips.setTextColor(Color.RED)
                mRePasswordTips.visibility = View.VISIBLE
                mRePassword.setCompoundDrawables(null, null, right, null)
            } else {
                val right = ResourcesCompat.getDrawable(
                    resources,
                    R.mipmap.ic_appropriate,
                    null
                )
                right?.setBounds(0, 0, mRePassword.height / 2, mRePassword.height / 2)
                mRePasswordTips.visibility = View.GONE
                mRePassword.setCompoundDrawables(null, null, right, null)
            }
        }
        mRegister.isEnabled = mUsernameState && mPasswordState && same
    }


    companion object {
        private const val TAG = "LC_RegisterFragment"
    }
}